package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * @author Macross
 * @Type ArticleSearchService.java
 * @Desc 文章搜索Service服务类
 * @date 2019/12/19 15:21
 */
@Service
public class ArticleSearchService {

    @Autowired
    private ArticleSearchDao articleSearchDao;
    @Autowired
    private IdWorker idWorker;


    /**
     * 通过关键字搜索文章
     * @param keyword 搜索关键字
     * @param page 分页页码
     * @param size 分页大小
     * @return
     */
    public PageResult<Article> findByTitleOrContentLike(String keyword, Integer page, Integer size) {
        //获取搜索结果
        Page<Article> articles = articleSearchDao.findByTitleOrContentLike(keyword, keyword, PageRequest.of(page - 1, size));
        //将结果进行封装并返回
        return new PageResult<>(articles.getTotalElements(),articles.getContent());
    }

    /**
     * 添加文章信息到ES
     * @param article 文章信息
     */
    public void add(Article article) {
        article.setId(idWorker.nextId()+"");
        articleSearchDao.save(article);
    }
}
